package com.sgzn.sgpay.service.impl;

import com.sgzn.sgpay.model.request.SgsTodayIncomeRequest;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.model.response.result.SgsTodayIncomeResult;
import com.sgzn.sgpay.persistence.dao.TBizOrderReadMapper;
import com.sgzn.sgpay.persistence.domain.TDevice;
import com.sgzn.sgpay.persistence.manager.DeviceDaoManager;
import com.sgzn.sgpay.persistence.manager.PresentReadManager;
import com.sgzn.sgpay.persistence.manager.ShopDaoManager;
import com.sgzn.sgpay.persistence.request.TBizOrderReadShopRequest;
import com.sgzn.sgpay.persistence.response.TBizOrderRead;
import com.sgzn.sgpay.persistence.response.TPresentRead;
import com.sgzn.sgpay.service.SgsTodayIncomeService;
import com.sgzn.sgpay.service.support.SgsResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 今日收益服务接口实现类
 * @author huangsenguang
 * @version Id: SgsTadayIncomeServiceImpl.java, v 0.1 2018/6/10 11:29 huangsenguang Exp $$
 */
@Service
@Slf4j
public class SgsTodayIncomeServiceImpl implements SgsTodayIncomeService {
    @Resource
    private TBizOrderReadMapper tBizOrderReadMapper;

    @Autowired
    private ShopDaoManager shopDaoManager;

    @Autowired
    private PresentReadManager presentReadManager;

    @Resource
    private DeviceDaoManager deviceDaoManager;

    @Override
    public SgsResultResponse<SgsTodayIncomeResult> query(SgsTodayIncomeRequest request) {
        return SgsResponseUtil.INSTANCE.success(getSgsTodayIncomeResult(request.getMchId()));
    }

    /**
     * 组装结果响应
     * @param merchantId
     * @return
     */
    private SgsTodayIncomeResult getSgsTodayIncomeResult(String merchantId) {
        SgsTodayIncomeResult result = new SgsTodayIncomeResult();
        result.setConsumePresentAmount(getTotalPresentAmount(merchantId));
        List<String> shopIdList = this.shopDaoManager.getShopIdList(merchantId);
        if (CollectionUtils.isEmpty(shopIdList)) {
            result.setOnlineAmount(new BigDecimal(0));
        } else {
            result.setOnlineAmount(getOnlineAmountMap(shopIdList));
        }
        result.setCashIncome(new BigDecimal(0));//未做
        result.setAdIncome(new BigDecimal(0));//未做
        result.setSlitCoinQuantity(0);//未做
        return result;
    }

    /**
     * 礼品消耗总个数
     * @param merchantId
     * @return
     */
    private int getTotalPresentAmount(String merchantId) {
        List<TDevice> tDeviceList = this.deviceDaoManager.getDevicesByMch(merchantId);
        int totalPresentAmount = 0;
        if (!CollectionUtils.isEmpty(tDeviceList)) {
            List<String> deviceIdList = new ArrayList<>(tDeviceList.size());
            for (TDevice tDevice : tDeviceList) {
                deviceIdList.add(tDevice.getDeviceId());
            }
            List<TPresentRead> records = presentReadManager.gettPresentReads(deviceIdList);
            for (TPresentRead record : records) {
                totalPresentAmount = Integer.sum(totalPresentAmount, record.getPresentAmount());
            }
        }
        return totalPresentAmount;
    }

    /**
     * 在线支付
     * @param shopIdList
     * @return
     */
    private BigDecimal getOnlineAmountMap(List<String> shopIdList) {
        TBizOrderReadShopRequest tBizOrderReadShopRequest = new TBizOrderReadShopRequest();
        tBizOrderReadShopRequest.setShopIdList(shopIdList);
        List<TBizOrderRead> respList = tBizOrderReadMapper.queryByShopIds(tBizOrderReadShopRequest);
        BigDecimal onlineAmount = new BigDecimal(0);
        for (TBizOrderRead tBizOrderRead : respList) {
            onlineAmount.add(tBizOrderRead.getSumAmount());
        }
        return onlineAmount;
    }
}

